package service

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"smart-sensor-cloud/internal/model"
	"smart-sensor-cloud/pkg/util"
	"strconv"
)

func GetMenus(ctx *gin.Context) {
	sizeStr := ctx.DefaultQuery("size", "10")
	indexStr := ctx.DefaultQuery("page", "1")

	size, err := strconv.Atoi(sizeStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}
	index, err := strconv.Atoi(indexStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}

	if results, total, err := model.GetMenus(index, size); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: "查询菜单列表失败",
		})
		return
	} else {
		util.ResponseOK(ctx, util.OKWithPayload(&util.PaginationResp{
			Size:  size,
			Index: index,
			Data:  results,
			Total: total,
		}))
	}
}

func GetMenuWithID(ctx *gin.Context) {
	menuIDStr := ctx.Param("id")
	if menuIDStr == "" {
		ctx.Set("error", util.NewInvalidParameterError("菜单ID不可以为空"))
		return
	}

	menuID, err := strconv.Atoi(menuIDStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("非法的参数"))
		return
	}

	menu, err := model.GetMenuWithID(menuID)
	if err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OKWithPayload(menu))
}

func GetMenuTree(ctx *gin.Context) {
	sizeStr := ctx.DefaultQuery("size", "10")
	indexStr := ctx.DefaultQuery("page", "1")

	size, err := strconv.Atoi(sizeStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}
	index, err := strconv.Atoi(indexStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}

	menus, err := model.GetAllMenus(nil)
	if err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}
	menuTrees := menus.ToTree()

	util.ResponseOK(ctx, util.OKWithPayload(&util.PaginationResp{
		Size:  size,
		Index: index,
		Data:  menuTrees,
		Total: len(menuTrees),
	}))

}

func AddMenu(ctx *gin.Context) {
	menu := new(model.Menu)

	if err := ctx.MustBindWith(menu, binding.JSON); err != nil {
		ctx.Set("error", util.NewInvalidParameterError(err.Error()))
		return
	}

	if err := model.AddMenu(menu); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OK())
}

func UpdateMenu(ctx *gin.Context) {
	menuIDStr := ctx.Param("id")

	menuID, err := strconv.Atoi(menuIDStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}

	change := new(model.Menu)
	if err := ctx.MustBindWith(change, binding.JSON); err != nil {
		ctx.Set("error", util.NewInvalidParameterError(err.Error()))
		return
	}

	if err := model.UpdateMenu(menuID, change); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OK())
}

func DeleteMenu(ctx *gin.Context) {
	menuIDStr := ctx.Param("id")

	menuID, err := strconv.Atoi(menuIDStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}

	if err := model.DeleteMenu(menuID); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OK())
}
